<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Upload extends Base_Controller {

    function __construct ()
    {
        parent::__construct();
        $this->load->model('upload_m');
        $this->load->model('attachs_m');
//        if(!$this->auth->is_login())
//        {
//            die('无权访问此页');
//        }
    }

    public function upload_book(){
        $access_key = $this->input->get('access_key',TRUE);
        if(empty($access_key)){
            $access_key = md5($this->session->userdata('user_id').time().uniqid());
        }
        $arr = array();
        $config['upload_path'] = FCPATH .'uploads/books/'.$access_key."/";
        $config['allowed_types'] = 'gif|jpg|png';
        $config['encrypt_name'] = TRUE;
        $config['max_size'] = 3000;
        if(!file_exists($config['upload_path'])){
            mkdir($config['upload_path'],0777,true);
        }
        $this->load->library('upload', $config);
        if(!$this->upload->do_upload('file')) {
            $arr = array ('status'=>4,'msg'=>'图片上传失败');
        }
        $upload_data = $this->upload->data();
        $attachs['file_name']=$upload_data['file_name'];
        $attachs['access_key']=$access_key;
        $attachs['add_time']=time();
        $attachs['file_location']='uploads/books/'.$access_key."/";
        $attachs['is_image']=1;
        $attachs['item_type']=1;//1为客服留言用
        $this->attachs_m->add_attachs($attachs);
        $url = base_url('uploads/books/'.$access_key.'/'.$upload_data['file_name']);
        $arr = array ('status'=>1,'msg'=>'图片上传成功','url'=>$url);
        echo json_encode($arr);
    }

    public function upload_order(){
        $order = $this->input->get('order',TRUE);
        if(empty($order)){
            $arr = array ('status'=>2,'msg'=>'图片上传失败');
        }
        $order_info = $this->order_m->get_order_by_id($order);
        if($order_info['order_lock']>1){
            echo json_encode(['status'=>2,"msg"=>"此单已经处理过,上传失败"]);exit();
        }
        $arr = array();
        $config['upload_path'] = FCPATH .'uploads/order/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['file_name'] = $order.'.jpg';
        $config['encrypt_name'] = false;
        $config['overwrite'] = true;
        $config['max_size'] = 3000;
        if(!file_exists($config['upload_path'])){
            mkdir($config['upload_path'],0777,true);
        }
        $this->load->library('upload', $config);
        if(!$this->upload->do_upload('file')) {
            $arr = array ('status'=>4,'msg'=>'图片上传失败');
        }
        $upload_data = $this->upload->data();
        $attachs['file_name']=$upload_data['file_name'];
        $attachs['access_key']=$order;
        $attachs['add_time']=time();
        $attachs['file_location']='uploads/order/'.$order."/";
        $attachs['is_image']=1;
        $attachs['item_type']=2;//2为订单凭证
        $this->attachs_m->add_attachs($attachs);
        $url = base_url('uploads/order/'.$upload_data['file_name'].'?t='.time());
        $arr = array ('status'=>1,'msg'=>'图片上传成功','url'=>$url);
        echo json_encode($arr);
    }

    public function upload_card(){
        $uid = $this->input->get('uid',TRUE);
        $name = $this->input->get('name',TRUE);
        $user_info = $this->user_m->get_user_by_user_id($uid);
        if(empty($uid)){
            $uid = $this->session->userdata('user_id');
        }
        $arr = array();
        if($user_info['user_card_rz']==2){
            $arr = array ('status'=>4,'msg'=>'您已经认证，修改失败');
        }else{
            $config['upload_path'] = FCPATH .'uploads/card/'.$uid."/";
            $config['allowed_types'] = 'gif|jpg|png';
            $config['file_name'] = $name.'.jpg';
            $config['encrypt_name'] = false;
            $config['overwrite'] = true;
            $config['max_size'] = 3000;

            if(!file_exists($config['upload_path'])){
                mkdir($config['upload_path'],0777,true);
            }
            $this->load->library('upload', $config);
            if(!$this->upload->do_upload(''.$name.'')) {
                $arr = array ('status'=>4,'msg'=>'图片上传失败');
            }else{
                $upload_data = $this->upload->data();
                $url = base_url('uploads/card/'.$uid.'/'.$upload_data['file_name'].'?t='.time());
                $arr = array ('status'=>1,'msg'=>'图片上传成功','url'=>$url);            
            }            
        }
        echo json_encode($arr);
    }

    /**
     * 删除图片
     * [deltet_img id]
     * @return [type] [description]
     */
    public function delete_img()
    {
        $attach_id = $this->input->post('key',TRUE);
        if($attach_id){
            $pic = $this->attachs_m->get_attachs_by_id($attach_id);
            if(file_exists(FCPATH .$pic['file_location'].$pic['file_name'])){
                unlink(FCPATH .$pic['file_location'].$pic['file_name']);
            }
            $del_res = $this->attachs_m->delete_attachs_by(['id'=>$attach_id]);
            $arr = array ('status'=>1,'msg'=>'图片删除成功');
            echo json_encode($arr);
            exit;
        }else{
            return false;
        }
    }

    /**
     * 单张上传
     */
    public function upload_pic()
    {
        $uid = 1;
        $access_key = $this->input->get('access_key',TRUE);
        $name = $this->input->get('name',TRUE);
        $uniqid = md5('shop_auth'.time().uniqid());
        $config['upload_path'] = FCPATH .'uploads/'.$uid.'/';
        $config['allowed_types'] = 'gif|jpg|png|JPG|PNG|JPEG|GIF|bmp|jpeg';
        $config['file_name'] = $uniqid."_".$name.'.jpg';
        $config['encrypt_name'] = false;
        $config['overwrite'] = true;
        $config['max_size'] = 3000;
        if(!file_exists($config['upload_path'])){
            mkdir($config['upload_path'],0777,true);
        }
        $this->load->library('upload', $config);
        if(!$this->upload->do_upload(''.$name.'')) {
            $arr = array ('status'=>4,'msg'=>'图片上传失败');
        }else{
            $upload_data = $this->upload->data();
            $attachs['file_name']=$upload_data['file_name'];
            $attachs['access_key']=$access_key;
            $attachs['add_time']=time();
            $attachs['file_location']='uploads/goods/'.$access_key."/";
            $attach_id = $this->attachs_m->add_attachs($attachs);
            $url = base_url('uploads/'.$uid.'/'.$upload_data['file_name'].'?t='.time());
            $arr = array ('status'=>1,'msg'=>'图片上传成功','url'=>$url,"attach_id"=>$attach_id);
        }
        echo json_encode($arr);
    }
}
